﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Ket_noi_toi_VPP_bang_mo_hinh_ba_lop.Connection;

namespace Ket_noi_toi_VPP_bang_mo_hinh_ba_lop.DAL
{
    class SanPham
    {
        private dbConnection Conn;

        public SanPham()
        {
            Conn = new dbConnection();
        }

        public bool Insert(string MaNhom, string MaNCC, string TenSP, int SoLuongHienCo, int DonGia)
        { 
            SqlParameter[] sqlPara=new SqlParameter[5];
            sqlPara[0] = new SqlParameter("@MaNhom", SqlDbType.NVarChar, 20);
            sqlPara[0].Value = Convert.ToString(MaNhom);
            sqlPara[1] = new SqlParameter("@MaNCC", SqlDbType.NVarChar, 20);
            sqlPara[1].Value = Convert.ToString(MaNCC);
            sqlPara[2] = new SqlParameter("@TenSP",SqlDbType.NVarChar,40);                        
            sqlPara[2].Value = Convert.ToString(TenSP);          
            sqlPara[3] = new SqlParameter("@SoLuonghienCo", SqlDbType.Int);
            sqlPara[3].Value = SoLuongHienCo;           
            sqlPara[4] = new SqlParameter("@DonGia", SqlDbType.Int);
            sqlPara[4].Value = DonGia;           
            return Conn.executeInsertProcedureQuery("Insert_SP", sqlPara);
        }

        public bool Update(int MaSP, string MaNhom, string MaNCC, string TenSP, int SoLuongHienCo, int DonGia)
        {
            SqlParameter[] sqlPara = new SqlParameter[6];
            sqlPara[0] = new SqlParameter("@MaSP", SqlDbType.Int);
            sqlPara[0].Value = MaSP;
            sqlPara[1] = new SqlParameter("@MaNhom", SqlDbType.NVarChar, 20);
            sqlPara[1].Value = Convert.ToString(MaNhom);
            sqlPara[2] = new SqlParameter("@MaNCC", SqlDbType.NVarChar, 20);
            sqlPara[2].Value = Convert.ToString(MaNCC);
            sqlPara[3] = new SqlParameter("@TenSP", SqlDbType.NVarChar, 40);
            sqlPara[3].Value = Convert.ToString(TenSP);
            sqlPara[4] = new SqlParameter("@SoLuonghienCo", SqlDbType.Int);
            sqlPara[4].Value = SoLuongHienCo;
            sqlPara[5] = new SqlParameter("@DonGia", SqlDbType.Int);
            sqlPara[5].Value = DonGia;  
            return Conn.executeUpdateProcedureQuery("Update_SP",sqlPara);
        }
        
        public bool Delete(int MaSP)
        { 
            SqlParameter[] sqlPara=new SqlParameter[1];
            sqlPara[0] = new SqlParameter("@MaSP",SqlDbType.Int);
            sqlPara[0].Value = MaSP;
            return Conn.executeUpdateProcedureQuery("Delete_SP",sqlPara);
        }
        
        public DataTable SelectAll()
        {
            return Conn.excuteSelectProcedureQuery("SelectAll_SP");
        }

        public DataTable SelectID(int MaSP)
        { 
            SqlParameter[] sqlPara=new SqlParameter[1];
            sqlPara[0] = new SqlParameter("@MaSP",SqlDbType.Int);
            sqlPara[0].Value = MaSP;
            return Conn.excuteSelectProcedureQuery("SelectID_SP",sqlPara);
        }
    }
}
